2020-2021 Sunseeker Telemetry and Lighting System
eusci_a_spi.c
Go to the documentation of this file.
1 //*****************************************************************************
2 //
3 // eusci_a_spi.c - Driver for the eusci_a_spi Module.
4 //
5 //*****************************************************************************
6 
7 //*****************************************************************************
8 //
11 //
12 //*****************************************************************************
13 
14 #include "inc/hw_memmap.h"
15 
16 #ifdef __MSP430_HAS_EUSCI_Ax__
17 #include "eusci_a_spi.h"
18 
19 #include <assert.h>
20 
21 void EUSCI_A_SPI_initMaster (uint16_t baseAddress,
22  EUSCI_A_SPI_initMasterParam *param)
23 {
24  //Disable the USCI Module
25  HWREG16(baseAddress + OFS_UCAxCTLW0) |= UCSWRST;
26 
27  //Reset OFS_UCAxCTLW0 values
28  HWREG16(baseAddress + OFS_UCAxCTLW0) &= ~(UCCKPH + UCCKPL + UC7BIT + UCMSB +
29  UCMST + UCMODE_3 + UCSYNC);
30 
31  //Reset OFS_UCAxCTLW0 values
32  HWREG16(baseAddress + OFS_UCAxCTLW0) &= ~(UCSSEL_3);
33 
34  //Select Clock
35  HWREG16(baseAddress + OFS_UCAxCTLW0) |= (uint16_t)param->selectClockSource;
36 
37  HWREG16(baseAddress + OFS_UCAxBRW) =
38  (uint16_t)(param->clockSourceFrequency / param->desiredSpiClock);
39 
40  /*
41  * Configure as SPI master mode.
42  * Clock phase select, polarity, msb
43  * UCMST = Master mode
44  * UCSYNC = Synchronous mode
45  * UCMODE_0 = 3-pin SPI
46  */
47  HWREG16(baseAddress + OFS_UCAxCTLW0) |= (
48  param->msbFirst +
49  param->clockPhase +
50  param->clockPolarity +
51  UCMST +
52  UCSYNC +
53  param->spiMode
54  );
55  //No modulation
56  HWREG16(baseAddress + OFS_UCAxMCTLW) = 0;
57 }
58 
59 void EUSCI_A_SPI_select4PinFunctionality (uint16_t baseAddress,
60  uint16_t select4PinFunctionality
61  )
62 {
63  HWREG16(baseAddress + OFS_UCAxCTLW0) &= ~UCSTEM;
64  HWREG16(baseAddress + OFS_UCAxCTLW0) |= select4PinFunctionality;
65 }
66 
67 void EUSCI_A_SPI_changeMasterClock (uint16_t baseAddress,
68  EUSCI_A_SPI_changeMasterClockParam *param)
69 {
70  //Disable the USCI Module
71  HWREG16(baseAddress + OFS_UCAxCTLW0) |= UCSWRST;
72 
73  HWREG16(baseAddress + OFS_UCAxBRW) =
74  (uint16_t)(param->clockSourceFrequency / param->desiredSpiClock);
75 
76  //Reset the UCSWRST bit to enable the USCI Module
77  HWREG16(baseAddress + OFS_UCAxCTLW0) &= ~(UCSWRST);
78 }
79 
80 void EUSCI_A_SPI_initSlave (uint16_t baseAddress, EUSCI_A_SPI_initSlaveParam *param)
81 {
82  //Disable USCI Module
83  HWREG16(baseAddress + OFS_UCAxCTLW0) |= UCSWRST;
84 
85  //Reset OFS_UCAxCTLW0 register
86  HWREG16(baseAddress + OFS_UCAxCTLW0) &= ~(UCMSB +
87  UC7BIT +
88  UCMST +
89  UCCKPL +
90  UCCKPH +
91  UCMODE_3
92  );
93 
94  //Clock polarity, phase select, msbFirst, SYNC, Mode0
95  HWREG16(baseAddress + OFS_UCAxCTLW0) |= (param->clockPhase +
96  param->clockPolarity +
97  param->msbFirst +
98  UCSYNC +
99  param->spiMode
100  );
101 }
102 
103 void EUSCI_A_SPI_changeClockPhasePolarity (uint16_t baseAddress,
104  uint16_t clockPhase,
105  uint16_t clockPolarity
106  )
107 {
108  //Disable the USCI Module
109  HWREG16(baseAddress + OFS_UCAxCTLW0) |= UCSWRST;
110 
111  HWREG16(baseAddress + OFS_UCAxCTLW0) &= ~(UCCKPH + UCCKPL);
112 
113  HWREG16(baseAddress + OFS_UCAxCTLW0) |= (
114  clockPhase +
115  clockPolarity
116  );
117 
118  //Reset the UCSWRST bit to enable the USCI Module
119  HWREG16(baseAddress + OFS_UCAxCTLW0) &= ~(UCSWRST);
120 }
121 
122 void EUSCI_A_SPI_transmitData ( uint16_t baseAddress,
123  uint8_t transmitData
124  )
125 {
126  HWREG16(baseAddress + OFS_UCAxTXBUF) = transmitData;
127 }
128 
129 uint8_t EUSCI_A_SPI_receiveData (uint16_t baseAddress)
130 {
131  return ( HWREG16(baseAddress + OFS_UCAxRXBUF)) ;
132 }
133 
134 void EUSCI_A_SPI_enableInterrupt (uint16_t baseAddress,
135  uint16_t mask
136  )
137 {
138  HWREG16(baseAddress + OFS_UCAxIE) |= mask;
139 }
140 
141 void EUSCI_A_SPI_disableInterrupt (uint16_t baseAddress,
142  uint16_t mask
143  )
144 {
145  HWREG16(baseAddress + OFS_UCAxIE) &= ~mask;
146 }
147 
148 uint8_t EUSCI_A_SPI_getInterruptStatus (uint16_t baseAddress,
149  uint8_t mask
150  )
151 {
152  return ( HWREG16(baseAddress + OFS_UCAxIFG) & mask );
153 }
154 
155 void EUSCI_A_SPI_clearInterrupt (uint16_t baseAddress,
156  uint16_t mask
157  )
158 {
159  HWREG16(baseAddress + OFS_UCAxIFG) &= ~mask;
160 }
161 
162 void EUSCI_A_SPI_enable (uint16_t baseAddress)
163 {
164  //Reset the UCSWRST bit to enable the USCI Module
165  HWREG16(baseAddress + OFS_UCAxCTLW0) &= ~(UCSWRST);
166 }
167 
168 void EUSCI_A_SPI_disable (uint16_t baseAddress)
169 {
170  //Set the UCSWRST bit to disable the USCI Module
171  HWREG16(baseAddress + OFS_UCAxCTLW0) |= UCSWRST;
172 }
173 
174 uint32_t EUSCI_A_SPI_getReceiveBufferAddress (uint16_t baseAddress)
175 {
176  return ( baseAddress + OFS_UCAxRXBUF );
177 }
178 
179 uint32_t EUSCI_A_SPI_getTransmitBufferAddress (uint16_t baseAddress)
180 {
181  return ( baseAddress + OFS_UCAxTXBUF );
182 }
183 
184 uint16_t EUSCI_A_SPI_isBusy (uint16_t baseAddress)
185 {
186  //Return the bus busy status.
187  return (HWREG16(baseAddress + OFS_UCAxSTATW) & UCBUSY);
188 }
189 
190 
191 #endif
192 //*****************************************************************************
193 //
196 //
197 //*****************************************************************************
uint8_t transmitData
MPU_initThreeSegmentsParam param
#define HWREG16(x)
Definition: hw_memmap.h:39